using Microsoft.AspNetCore.Mvc;
using System.ComponentModel.DataAnnotations;
using System.Text;
using System.Text.Json;

namespace TaskProjectMVC.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class BOMController : ControllerBase
    {
        private readonly ILogger<BOMController> _logger;

        public BOMController(ILogger<BOMController> logger)
        {
            _logger = logger;
        }

        [HttpPost("AddBOM")]
        public async Task<IActionResult> AddBOM([FromBody] AddBOMRequest request)
        {
            try
            {
                _logger.LogInformation("收到新增BOM请求: {@Request}", request);

                // 调用真实的后端API
                using var httpClient = new HttpClient();
                
                // 根据您的后端API地址进行修改
                var apiUrl = "http://localhost:5025/api/BOM/AddBOM"; // 请根据实际情况修改端口
                
                var jsonContent = JsonSerializer.Serialize(request);
                var content = new StringContent(jsonContent, Encoding.UTF8, "application/json");
                
                var response = await httpClient.PostAsync(apiUrl, content);
                var responseContent = await response.Content.ReadAsStringAsync();
                
                _logger.LogInformation("后端API响应: {Response}", responseContent);
                
                if (response.IsSuccessStatusCode)
                {
                    return Ok(JsonSerializer.Deserialize<object>(responseContent));
                }
                else
                {
                    return BadRequest(JsonSerializer.Deserialize<object>(responseContent));
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "新增BOM失败");
                return BadRequest(new
                {
                    msg = "新增BOM失败: " + ex.Message,
                    code = 400
                });
            }
        }

        [HttpGet("GetProduct")]
        public async Task<IActionResult> GetProduct()
        {
            try
            {
                // 调用真实的后端API
                using var httpClient = new HttpClient();
                
                // 根据您的后端API地址进行修改
                var apiUrl = "http://localhost:5025/api/BOM/GetProduct"; // 请根据实际情况修改端口
                
                var response = await httpClient.GetAsync(apiUrl);
                var responseContent = await response.Content.ReadAsStringAsync();
                
                _logger.LogInformation("获取产品API响应: {Response}", responseContent);
                
                if (response.IsSuccessStatusCode)
                {
                    return Ok(JsonSerializer.Deserialize<object>(responseContent));
                }
                else
                {
                    return BadRequest(JsonSerializer.Deserialize<object>(responseContent));
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取产品数据失败");
                return BadRequest(new
                {
                    msg = "获取产品数据失败: " + ex.Message,
                    code = 400
                });
            }
        }

        [HttpGet("GetMaterial")]
        public async Task<IActionResult> GetMaterial()
        {
            try
            {
                // 调用真实的后端API
                using var httpClient = new HttpClient();
                
                // 根据您的后端API地址进行修改
                var apiUrl = "http://localhost:5025/api/BOM/GetMaterial"; // 请根据实际情况修改端口
                
                var response = await httpClient.GetAsync(apiUrl);
                var responseContent = await response.Content.ReadAsStringAsync();
                
                _logger.LogInformation("获取物料API响应: {Response}", responseContent);
                
                if (response.IsSuccessStatusCode)
                {
                    return Ok(JsonSerializer.Deserialize<object>(responseContent));
                }
                else
                {
                    return BadRequest(JsonSerializer.Deserialize<object>(responseContent));
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取物料数据失败");
                return BadRequest(new
                {
                    msg = "获取物料数据失败: " + ex.Message,
                    code = 400
                });
            }
        }
    }

    public class AddBOMRequest
    {
        [Required(ErrorMessage = "BOM主题是必需的")]
        public string bomTheme { get; set; } = string.Empty;

        [Required(ErrorMessage = "BOM版本是必需的")]
        public string bomVersion { get; set; } = string.Empty;

        [Required(ErrorMessage = "产品ID是必需的")]
        public int productId { get; set; }

        [Required(ErrorMessage = "物料ID是必需的")]
        public int materialId { get; set; }

        [Required(ErrorMessage = "天数是必需的")]
        public int dayNum { get; set; }

        [Required(ErrorMessage = "BOM描述是必需的")]
        public string bomDesc { get; set; } = string.Empty;

        [Required(ErrorMessage = "零件数量是必需的")]
        public int partNum { get; set; }

        [Required(ErrorMessage = "损耗率是必需的")]
        public string lossRate { get; set; } = string.Empty;

        public bool bomState { get; set; } = true;
    }
} 